#include "include/MessageParser.h"
#include "util/base64.h"
#include "include/rdMessage.h"
#include "core/Utility.h"
#include "include/ssLoginProto.h"
#include "include/SSQBModel.h"
#include "include/SSMoneyMarketModel.h"
#include "include/SSModel.h"

#define min(a,b)            (((a) < (b)) ? (a) : (b))

namespace qb
{

MessageParser::MessageParser(void):sMsgBuffer(NULL),nIsStillReceving(0)
{
	m_time_decode = 0.0;
	m_time_extract = 0.0;
	m_time_clean = 0.0;
	m_msg_length = 0;
	m_funcid = 0;
}

MessageParser::~MessageParser(void)
{
	sMsgBuffer = NULL;
}

void MessageParser::Clean()
{
	static double total_time = 0;
	static double total_count = 0;
	msg.Clear();
	total_time += m_time_decode + m_time_extract + m_time_clean;
	total_count += 1;

	nIsStillReceving = 0;
	m_time_decode = 0.0;
	m_time_extract = 0.0;
	m_time_clean = 0.0;
	m_msg_length = 0;
	m_funcid = 0;
	memset(&msgheader,0,sizeof(AckMsgHead));
}

void MessageParser::ExtractHeader(const ProtoMessage &msg)
{
	msgheader.retcode = 1;
	ProtoMap header;
	if(msg.GetMap("AckMsgHead",header))
	{
		msgheader.total = msgheader.idx = 0;
		header.getInt16("retcode",msgheader.retcode);
		header.getInt16("total",msgheader.total);
		header.getInt16("idx",msgheader.idx);
		FIELDCOPY(msgheader.desc, CParserTool::GetString(header,"desc"));
		header.GetFloat("exectime",msgheader.exectime);
		if(msgheader.idx+1<msgheader.total)
			nIsStillReceving = 1;
		else
			nIsStillReceving = 0;
	}
}

void* MessageParser::ExtractData(int funcId, const ProtoMessage& cmsg)
{
	ExtractHeader(msg);

	ProtoMap msgoutcome;
	if (!cmsg.GetMap("AckMsgBody", msgoutcome))
		return nullptr;

	return m_decodeFunc(funcId, WrapProtoMsg(msgoutcome), nullptr);
}

bool MessageParser::JudgeJavaReturnInfo(const ProtoMessage &msg)
{
	int res = -1;			//0--执行成功
	float fSeconds = 0.0;	//服务端执行时间,秒数
	std::string strError;	//错误信息

	msg.GetInt32("res",res);
	msg.GetFloat("q",fSeconds);
	//msg.CParserTool::GetString("e",strError);
	strError = CParserTool::GetString(msg, "e");
	if(res==0)
		return true;
	else
	{
		//OutputDebugString(strError.c_str());
		return false;
	}
}

int MessageParser::GetMessageType(const char* sBuffer, int nLen,SSAckMsg& stAckMsg)
{
	m_current_reqid = 0;
	stAckMsg.m_Count = 1;
	int iFuncID = 0;
	if( sBuffer )
	{
		sMsgBuffer = const_cast<char*>(sBuffer);
		int ret = msg.decode(sBuffer, nLen);
		m_msg_length = nLen;
		qb::base::msg::ProtoUtil::CheckMessage(msg, sBuffer, nLen);

		//get RequestID
		stAckMsg.m_RequestID = atoi(msg.getMessageID().c_str());
		m_current_reqid = stAckMsg.m_RequestID;
		iFuncID = CParserTool::GetFuncIDbyType(msg.getType());
		m_funcid = iFuncID;

		return iFuncID;
	}
	else
		return iFuncID;
}
bool MessageParser::GetCurrentMessage(int reqid, const ProtoMessage*& pmsg)
{
	if(m_current_reqid==reqid || reqid==0)
	{
		pmsg = &msg;
		return true;
	}
	return false;
}
bool MessageParser::GetCurrentHead(AckMsgHead& head)
{
	head = msgheader;
	return true;
}

void* MessageParser::DecodeMessage(SSAckMsg* pMsg)
{
	if(!sMsgBuffer || m_decodeFunc == nullptr)
		return nullptr;

	nIsStillReceving = 0;
	pMsg->m_MsgType = SSReqType_Message;
	void* pdata = ExtractData(pMsg->m_FuncID, msg);
	//----------------------------------------------------------------------------
	pMsg->m_ErrorCode = msgheader.retcode;
	pMsg->m_Index = msgheader.idx;
	pMsg->m_Count = msgheader.total;
	pMsg->m_IsReceving = nIsStillReceving;//用于判断是否结束接收新包
	FIELDCOPY(pMsg->m_Desc, msgheader.desc);				// 错误信息

	pMsg->m_pRet = pdata;
	return pdata;

}

void MessageParser::SetDecodeFunc(DecodeFunc f)
{
	m_decodeFunc = f;
}
}
